<?php
global $base_dir;
$base_dir='../../';

/**** 模块调用*****/
include_once($base_dir."inc/inc_connect.php");		//通用函数包含文件
?>
<?php
include_once($base_dir."api/file_api.php");			//文件处理接口
/**
* 获取省的相关信息
* @return Array
*/
function getProvinceInfo()
{
	$conv=" where status=1";
	$conv.=" order by name asc";
	$t=new Tbl_Act('area_province','id');
	$rows=$t->GetAll('code,name',$conv);
	return $rows;
}

/**
* 获取省id获取市的相关信息
* @param provinceCode		省邮政编码
* @return Array
*/
function getCityInfo($provinceCode)
{
	$conv=" where status=1";
	if($provinceCode>0)
	{
		$conv.=" and provinceId='$provinceCode'";
	}
	$conv.=" order by name asc";
	$t=new Tbl_Act('area_city','id');
	$rows=$t->GetAll('code,name',$conv);
	return $rows;
}

/**
* 获取省id获取县的相关信息
* @param areaCode		省邮政编码
* @return Array
*/
function getAreaInfo($areaCode)
{
	$conv=" where status=1";
	if($areaCode>0)
	{
		$conv.=" and cityId='$areaCode'";
	}
	$conv.=" order by name asc";
	$t=new Tbl_Act('area_area','id');
	$rows=$t->GetAll('code,name',$conv);
	return $rows;
}

function creatCityJs()
{
	global $base_dir;	//页面头部定义相对根目录路径
	$aryProvince = getProvinceInfo();
	
	if(is_array($aryProvince))
	{
		//省
		$strProvince='function getProvince(){'."\n";
		$strProvince.='	var ss=new Array()'.";\n";
		
		//市
		$strCity='function getCity(pid){'."\n";
		$strCity.='	var ss=new Array()'.";\n";
		
		//县
		$strArea='function getArea(cid){'."\n";
		$strArea.='	var ss=new Array()'.";\n";
		foreach($aryProvince as $key => $Province)
		{
			$strProvince.='	ss['.$key.']=new Array()'.";\n";
			$strProvince.='	ss['.$key.']["name"]="'.$Province['name'].'"'.";\n";
			$strProvince.='	ss['.$key.']["code"]="'.$Province['code'].'"'.";\n";
		
			$aryCity = getCityInfo($Province['code']);
			if(is_array($aryCity))
			{
				$strCity.='	ss["'.$Province['code'].'"]=new Array()'.";\n";
				foreach($aryCity as $key2 => $City)
				{
					$strCity.='	ss["'.$Province['code'].'"]['.$key2.']=new Array()'.";\n";
					$strCity.='	ss["'.$Province['code'].'"]['.$key2.']["name"]="'.$City['name'].'"'.";\n";
					$strCity.='	ss["'.$Province['code'].'"]['.$key2.']["code"]="'.$City['code'].'"'.";\n";
					
					$aryArea = getAreaInfo($City['code']);
					if(is_array($aryArea))
					{
						$strArea.='	ss["'.$City['code'].'"]=new Array()'.";\n";
						foreach($aryArea as $key3 => $Area)
						{
							$strArea.='	ss["'.$City['code'].'"]['.$key3.']=new Array()'.";\n";
							$strArea.='	ss["'.$City['code'].'"]['.$key3.']["name"]="'.$Area['name'].'"'.";\n";
							$strArea.='	ss["'.$City['code'].'"]['.$key3.']["code"]="'.$Area['code'].'"'.";\n";
						}
					}
				}
			}
		}
		$strProvince.='	return ss;'."\n";
		$strProvince.='}'."\n";
		
		$strCity.='	return ss[pid];'."\n";
		$strCity.='}'."\n";
		
		$strArea.='	return ss[cid];'."\n";
		$strArea.='}'."\n";
		
		$ss.=$strProvince;
		$ss.=$strCity;
		$ss.=$strArea;
		
		//开始生成
		$path=$base_dir.'javascript/city/';
		$filename='cityinfo.js';
		$content=$ss;
		if(createfile($path,$filename,$content))
		{
			return true;
		}
		else
		{
			return false;
		}
		//生成结束
	}
}

if(creatCityJs())
{
	echo '城市脚本更新成功';
}
else
{
	echo '城市脚本更新失败';
}
?>